﻿Public Class man_kits
    'poarametros para mantenedor
    Dim tituloVentna As String = "Mantenedor de kits"
    Dim columnas() As String = {"N.kit-80", "N.Serie-80", "Patologia-150", "Estudio-150", "Lote-130", "Estado-130", "Fecha Ingreso-130", "Fecha Vencimiento-130", "Ubicación-230"} 'separar por - numero:largo
    Dim comboItems() As String = {"Numero de kit", "Numero de serie", "Patologia", "Numero estudio", "Lote"}
    Dim nombreTabla As String = "kit"
    Dim idTabla As String = "kit_id"
    'Dim qryOriginal = "select est_id,est_nombre, (inve_nombre +' '+ inve_apellido)as nombre, est_patologia, est_fecha_inicio, est_fecha_termino, CASE WHEN est_estado = 1 THEN 'activo' ELSE 'inactivo' END as estado from ESTUDIOS inner join INVESTIGADORES on INVESTIGADORES.INVE_ID=ESTUDIOS.EST_ID"
    Dim qryOriginal = "select kit_id,kit_idex,(select pat_nombre from patologias where pat_id=estudios.pat_id)as pat_nombre,est_nombre,kit_lote,etdo_nombre,Convert(Varchar(10),kit_fecha_ingreso,105)as kit_fecha_ingreso, Convert(Varchar(10),kit_fecha_vencimiento,105)as kit_fecha_vencimiento,kit_ubicacion from kit inner join ESTUDIOS on ESTUDIOS.EST_ID=KIT.EST_ID inner join estado_kit on estado_kit.etdo_id=KIT.etdo_id"

    'NO MODIFICAR
    Dim qry As String = qryOriginal
    Dim cadena As String = qry
    Dim ultima_qry As String
    Dim row As Integer
    Dim id As String
    Dim condicion As String
    Private Sub btn_cerrar_Click(sender As System.Object, e As System.EventArgs) Handles btn_cerrar.Click
        Me.Close()
    End Sub
    Public Sub actualizar()
        carga_lista(qryOriginal)
    End Sub
    Public Sub carga_columnas(ByVal columnas() As String)
        Dim col() As String
        ListView1.View = View.Details
        For Each cols As String In columnas
            col = Split(cols, "-")
            ListView1.Columns.Add(col(0), Integer.Parse(col(1)), HorizontalAlignment.Left)
        Next
    End Sub
    Dim fecha As New DateTimePicker
    Public Sub carga_lista(ByVal qry)

        ListView1.Items.Clear()
        ultima_qry = qry 'aca dejo la ultima ejecutada :D
        Dim dataset As New DataSet
        Consulta(qry, dataset)
        If dataset.Tables("data").Rows.Count = 0 Then
            ' MsgBox("No hay Registros", MsgBoxStyle.Information, "Error")
        Else
            With dataset.Tables("data")
                For f As Integer = 0 To .Rows.Count - 1
                    Dim dato As New ListViewItem(.Rows(f).Item(0).ToString)
                    ' recorrer las columnas  
                    For c As Integer = 1 To .Columns.Count - 1
                        dato.SubItems.Add(.Rows(f).Item(c).ToString())
                    Next
                    fecha.Value = dataset.Tables("data").Rows(f)("kit_fecha_vencimiento")

                    If fecha.Value.CompareTo(Date.Today) <= 0 Then
                        ListView1.Items.Add(dato).ForeColor = Color.Red
                    Else
                        ListView1.Items.Add(dato)
                    End If
                Next
            End With

        End If
    End Sub
    Public Sub carga_combo(ByVal items() As String)
        For Each item As String In items
            ComboBox1.Items.Add(item)
        Next
        ComboBox1.SelectedIndex = 0
    End Sub
    Public Sub inicia_mantenedor()
        With Me
            .Icon = (My.Resources.favicon)
            .Text = tituloVentna
        End With
        ' Me.Icon = (My.Resources.favicon)
        carga_columnas(columnas)
        carga_lista(qryOriginal)
        carga_combo(comboItems)
    End Sub
    Public Sub eliminar(ByRef id As String)
        qry = "delete from " & nombreTabla & " where " & idTabla & "='" & id & "'"
        If MsgBox("¿Esta seguro de eliminar este registro?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
            Try
                EjecutaConsulta(qry)
                actualizar()
            Catch ex As Exception
                MsgBox("Error en DB")
            End Try
        End If
    End Sub

    Private Sub btn_eliminar_Click(sender As System.Object, e As System.EventArgs) Handles btn_eliminar.Click
        Try
            row = ListView1.SelectedIndices.Item(0)
            id = ListView1.Items.Item(row).SubItems(0).Text
            eliminar(id)
            'registro movimiento
            registro("eliminar", login.usuario.id, main.modulo.kit)
        Catch
            MsgBox("Debe seleccionar un registro")
        End Try
    End Sub
    Private Sub Button5_Click(sender As System.Object, e As System.EventArgs) Handles btn_crear.Click
        Dim ng As New nuevo_kit
        ng.ShowDialog()
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles btn_cerrar.Click
        Me.Close()
    End Sub

    Private Sub man_kits_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        inicia_mantenedor()
    End Sub

    Private Sub Button6_Click(sender As System.Object, e As System.EventArgs) Handles btn_actualizar.Click
        actualizar()
    End Sub

    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles btn_imprimir.Click
        reporte.Owner = Me
        Dim rp As New rpt_kit
        reporte.carga_info(ultima_qry, rp)
    End Sub

    Private Sub Button8_Click(sender As System.Object, e As System.EventArgs) Handles btn_editar.Click
        Dim editar As New editar_kit

        Try
            row = ListView1.SelectedIndices.Item(0)
            id = ListView1.Items.Item(row).SubItems(0).Text
            editar.setid(id, "select * from " & nombreTabla & " where " & idTabla & "='" & id & "'")
            editar.ShowDialog()
        Catch
            MsgBox("Debe escoger un registro")
        End Try
    End Sub

    Private Sub buscar_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles buscar.KeyPress
        ValidarCaracteresEspeciales(e)
    End Sub
    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles btn_buscar.Click
        'Dim qry As String
        Dim condicion As String
        condicion = " where"
        Select Case ComboBox1.SelectedIndex
            Case 0
                qry = cadena & condicion & " kit_id = '" & buscar.Text & "'"
                carga_lista(qry)
            Case 1
                qry = cadena & condicion & " kit_idex = '" & buscar.Text & "'"
                carga_lista(qry)
            Case 2
                qry = cadena & condicion & " pat_nombre = '" & buscar.Text & "'"
                carga_lista(qry)
            Case 3
                qry = cadena & condicion & " est_id = '" & buscar.Text & "'"
                carga_lista(qry)
            Case 3
                qry = cadena & condicion & " kit_lote = '" & buscar.Text & "'"
                carga_lista(qry)
        End Select
        'ultima_qry = qry
        'carga_lista(cadena)
    End Sub

    Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles buscar.TextChanged
        If CheckBox1.Checked = True Then
            Dim condicion As String

            condicion = " where"

            Select Case ComboBox1.SelectedIndex
                Case 0
                    qry = cadena & condicion & " kit_id like '" & "%" & buscar.Text & "%" & "'"
                    carga_lista(qry)

                Case 1
                    qry = cadena & condicion & " kit_idex like  '" & "%" & buscar.Text & "%" & "'"
                    carga_lista(qry)

                Case 2
                    qry = cadena & condicion & " pat_nombre like  '" & "%" & buscar.Text & "%" & "'"
                    carga_lista(qry)
                Case 3
                    qry = cadena & condicion & " est_id like  '" & "%" & buscar.Text & "%" & "'"
                    carga_lista(qry)
                Case 4
                    qry = cadena & condicion & " kit_lote like  '" & "%" & buscar.Text & "%" & "'"
                    carga_lista(qry)
            End Select
        End If
    End Sub

    Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles btn_exportar.Click
        Dim dataset As New DataSet
        Consulta(ultima_qry, dataset)
        exel(dataset, ProgressBar1)
        'registro movimiento
        registro("exportar", login.usuario.id, main.modulo.kit)
    End Sub

    Private Sub vigentes_CheckedChanged(sender As System.Object, e As System.EventArgs)
        qry = cadena & " where est_fecha_termino >'" & Date.Today & "'"
        carga_lista(qry)

    End Sub

    Private Sub todos_CheckedChanged(sender As System.Object, e As System.EventArgs)
        actualizar()
    End Sub

    Private Sub terminados_CheckedChanged(sender As System.Object, e As System.EventArgs)
        qry = cadena & " where est_fecha_termino <'" & Date.Today & "'"
        carga_lista(qry)
    End Sub

    Private Sub GroupBox2_Enter(sender As System.Object, e As System.EventArgs)

    End Sub

    Private Sub RadioButton1_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RadioButton1.CheckedChanged
        condicion = " where kit.etdo_id='1'"
        qry = cadena & condicion
        carga_lista(qry)
    End Sub

    Private Sub RadioButton2_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RadioButton2.CheckedChanged
        condicion = " where kit.etdo_id='2'"
        qry = cadena & condicion
        carga_lista(qry)
    End Sub

    Private Sub RadioButton3_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RadioButton3.CheckedChanged
        condicion = " where kit.etdo_id='3'"
        qry = cadena & condicion
        carga_lista(qry)
    End Sub

    Private Sub RadioButton4_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RadioButton4.CheckedChanged
        condicion = " where kit.etdo_id='4'"
        qry = cadena & condicion
        carga_lista(qry)
    End Sub

    Private Sub RadioButton5_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RadioButton5.CheckedChanged
        carga_lista(qryOriginal)
    End Sub
End Class